package client;

import java.util.ArrayList;
import java.util.List;

import phasebook.client.msgs.UserProfileInfo;
import client.api.CombinedResponse;
import client.api.PhasebookServiceAsync;
import client.api.PhasebookServiceAsync.Util;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.SC;

public class Controller implements AsyncCallback<CombinedResponse> {
	private PhasebookServiceAsync async = Util.getInstance();

	@Override
	public void onFailure(Throwable caught) {
		SC.warn(caught.getMessage());
	}

	@Override
	public void onSuccess(CombinedResponse result) {
		System.out.println(handlers);
		int i=0;
		while(i<handlers.size())
			handlers.get(i++).onResponse(result);
	}
	
	public void updateLoop() {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				async.waitForRegions(new AsyncCallback<CombinedResponse>() {
					@Override
					public void onSuccess(CombinedResponse result) {
						updateLoop();
						Controller.this.onSuccess(result);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Controller.this.onFailure(caught);
					}
				});
			}
		});
	}

	public interface ResponseHandler {
		public void onResponse(CombinedResponse response);
	}

	private List<ResponseHandler> handlers = new ArrayList<ResponseHandler>();

	public void addResponseHandler(ResponseHandler handler) {
		handlers.add(handler);
	}
	

	public void removeResponseHandler(ResponseHandler handler) {
		handlers.remove(handler);
	}

	//API delegations

	public void findUsers(String regEx) {
		async.findUsers(regEx, this);
	}

	public void registerUser(UserProfileInfo user){
		async.registerUser(user, this);
	}

	public void validateLogin(String username, String password){
		async.validateLogin(username, password, this);
	}

	public void updateUser(UserProfileInfo user){
		async.updateUser(user, this);
	}

	public void updateBalance(double balanceUpdate, long accountId){
		async.updateBalance(balanceUpdate, accountId, this);
	}	
	
	public void publishMessage(long boardOwnerId, String message, boolean isPrivate, Long photoId){
		async.publishMessage(boardOwnerId, message, isPrivate, photoId, this);
	}

	public void deleteMessage(long messageId){
		async.deleteMessage(messageId, this);
	}

	public void requestFriendship(long friendId){
		async.requestFriendship(friendId, this);
	}

	public void cancelFriendship(long friendId){
		async.cancelFriendship(friendId, this);
	}

	public void cancelFriendshipRequest(long friendId){
		async.cancelFriendshipRequest(friendId, this);
	}

	public void denyFriendshipRequest(long friendId){
		async.denyFriendshipRequest(friendId, this);
	}

	public void acceptFriendshipRequest(long friendId){
		async.acceptFriendshipRequest(friendId, this);
	}

	public void placeBet(double ammount, int result){
		async.placeBet(ammount, result, this);
	}

	public void showUser(long userId) {
		async.trackUser(userId,this);
	}

	public void forgetUser(long userId) {
		async.untrackUser(userId,this);
	}

}
